/*
 * Copyright (c) 2006-2022, Ulandlink Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-08-10     zlj         first version
 */
#include <rtthread.h>
#include "drv_power_supply.h"
#include <pin.h>
#include "drv_gpio.h"

#ifndef LOG_TAG
#define LOG_TAG                    "drv.power_supply"
#endif
#undef LOG_LVL
#define LOG_LVL                    	LOG_LVL_INFO
#include "ulog.h"

#define  POWER_SUPPLY_USB3V3_CTR     GET_PIN(E,5)
#define  POWER_SUPPLY_USB5V_CTR      GET_PIN(B,6)
#define  POWER_SUPPLY_USB12V_CTR     GET_PIN(G,14)

static power_supply_e current_state;

int32_t power_supply_init(void)
{
	rt_thread_delay(2000);
	rt_pin_mode(POWER_SUPPLY_USB3V3_CTR, PIN_MODE_OUTPUT);
    rt_pin_mode(POWER_SUPPLY_USB5V_CTR, PIN_MODE_OUTPUT);
    rt_pin_mode(POWER_SUPPLY_USB12V_CTR, PIN_MODE_OUTPUT);

    rt_pin_write(POWER_SUPPLY_USB3V3_CTR, PIN_HIGH);
    rt_pin_write(POWER_SUPPLY_USB5V_CTR, PIN_LOW);
    rt_pin_write(POWER_SUPPLY_USB12V_CTR, PIN_HIGH);
    current_state = POWER_SUUPLY_USB5V;
    log_i("type-C output :5V\n");

    return 0;
}


void power_supply_control(power_supply_e new_state)
{
    if(new_state == current_state){
        return;
    }
    current_state = new_state;
    rt_pin_write(POWER_SUPPLY_USB3V3_CTR, PIN_HIGH);
    rt_pin_write(POWER_SUPPLY_USB5V_CTR, PIN_HIGH);
    rt_pin_write(POWER_SUPPLY_USB12V_CTR, PIN_HIGH);

    switch(new_state)
    {
        case POWER_SUUPLY_USB3V3:
            rt_pin_write(POWER_SUPPLY_USB3V3_CTR, PIN_LOW);
            log_i("usb power supply output 3V3\n");
            break;
        case POWER_SUUPLY_USB5V:
            rt_pin_write(POWER_SUPPLY_USB5V_CTR, PIN_LOW);
            log_i("usb power supply output 5V\n");
            break;
        case POWER_SUUPLY_USB12V:
            rt_pin_write(POWER_SUPPLY_USB12V_CTR, PIN_LOW);
            log_i("usb power supply output 12V\n");
            break;
        default:
            log_i("usb power supply output close\n");
            break;
    }

}

power_supply_e power_supply_get_state(void)
{
    return current_state;
}
#if defined(A5333_EDITION)
INIT_APP_EXPORT(power_supply_init);
#endif

#ifdef RT_USING_FINSH
static int power(int argc, char *argv[])
{
	int result = RT_EOK;
	if (argc >= 2){
		if ((strcmp(argv[1], "on") == 0)){
            if ((strcmp(argv[2], "3V3") == 0)){
                power_supply_control(POWER_SUUPLY_USB3V3);
            }else
            if ((strcmp(argv[2], "5V") == 0)){
                power_supply_control(POWER_SUUPLY_USB5V);
            }else
            if ((strcmp(argv[2], "12V") == 0)){
                power_supply_control(POWER_SUUPLY_USB12V);
            }else{
                rt_kprintf("command error,please input 'power' to get Usage info.\n");
            }
        }else
        if ((strcmp(argv[1], "off") == 0)){
            power_supply_control(POWER_SUUPLY_NONE);
        }else
        if ((strcmp(argv[1], "get") == 0)){
            switch(power_supply_get_state()){
                case POWER_SUUPLY_NONE:
                    rt_kprintf("The type-c output voltage now is 0V\n");
                    break;
                case POWER_SUUPLY_USB3V3:
                    rt_kprintf("The type-c output voltage now is 3V3\n");
                    break;
                case POWER_SUUPLY_USB5V:
                    rt_kprintf("The type-c output voltage now is 5V\n");
                    break;
                case POWER_SUUPLY_USB12V:
                    rt_kprintf("The type-c output voltage now is 12V\n");
                    break;
            }

        }else{
            rt_kprintf("command error,please input 'power' to get Usage info.\n");
        }

	} else {
		rt_kprintf("Usage:\n");
        rt_kprintf("power on 3V3/5V/12V ->set the type-c output voltage to 3V3/5V/12V\n");
        rt_kprintf("power off ->set the type-c output voltage to 0V\n");
        rt_kprintf("power get ->get the type-c output voltage state\n");
        rt_kprintf("\n");
	}
	return result; 
}

MSH_CMD_EXPORT(power, set type-c output voltage state);
#endif /* RT_USING_FINSH */


